package com.jxl.apple.components
{
	import com.bit101.components.InputText;
	import com.bit101.components.Label;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.FocusEvent;
	import flash.events.TextEvent;
	import flash.text.TextFormat;
	
	import mx.controls.TextInput;
	
	public class MenuInputText extends InputText
	{
		
		[Embed(source="/assets/images/menu-text-input/text-input-background.png", scaleGridLeft="12", scaleGridTop="12", scaleGridRight="277", scaleGridBottom="20")]
		private var TextInputBackgroundImage:Class;
		
		private var textInputBackground:Sprite;
		private var promptLabel:Label;
		private var _prompt:String = "";
		private var promptDirty:Boolean = true;
		
		public function get prompt():String { return _prompt; }
		public function set prompt(value:String):void
		{
			_prompt = value;
			promptDirty = true;
			invalidateProperties();
		}
		
		public function MenuInputText(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0, text:String="", defaultHandler:Function=null)
		{
			super(parent, xpos, ypos, text, defaultHandler);
		}
		
		protected override function addChildren():void
		{
			super.addChildren();
			
			textInputBackground = new TextInputBackgroundImage() as Sprite;
			addChildAt(textInputBackground, 0);
			
			promptLabel = new Label();
			addChild(promptLabel);
			var tf:TextFormat = promptLabel.textField.defaultTextFormat;
			tf.color = 0x666666;
			promptLabel.textField.defaultTextFormat = tf;
			
			this.textField.addEventListener(FocusEvent.FOCUS_IN, onTextFieldFocusIn);
			this.textField.addEventListener(FocusEvent.FOCUS_OUT, onTextFieldFocusOut);
		}
		
		protected override function commitProperties():void
		{
			super.commitProperties();
			
			if(promptDirty)
			{
				promptDirty = false;
				promptLabel.text = _prompt;
			}
		}
		
		public override function draw():void
		{
			super.draw();
			
			_back.graphics.clear();
			
			textInputBackground.width = width;
			
			promptLabel.move(5, 5);
			
			textField.y = 5;
		}
		
		private function onTextFieldFocusIn(event:FocusEvent):void
		{
			promptLabel.visible = false;
		}
		
		private function onTextFieldFocusOut(event:FocusEvent):void
		{
			if(text.length < 1)
				promptLabel.visible = true;
		}
	}
}